import { router } from "../../../../js/router";
import { coupon } from '../../../../js/modules/market';
import authorize from '../../../../js/modules/authorize.js';
import { api } from '../../../../js/util';
import {
  profile
} from '../../../../js/modules/store.js'
import BasePage from '../../../../js/common/base-page'
import { confirm } from "../../../../js/newUtil";
const app = getApp();
BasePage({
  pageKey: "receivedCouponList",
  validateLogin:false,
  /**
   * 页面的初始数据
   */
  data: {
    PageIndex: 1,
    PageSize: 20, 
    hasMoreData: true,
    couponList:[],
    productId:0,
    isIphoneX:app.globalData.isIphoneX,//适配手机
    Currency:'',//货币符号
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  this.setData({
    productId: options.ProductId||0
  });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () { 
    this.loadOnlineReceviedCoupons();
    this.setData({
      Currency:api.Currency || '￥'
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    this.data.PageIndex = this.data.PageIndex + 1;
    this.loadOnlineReceviedCoupons();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.hasMoreData) {
      this.loadOnlineReceviedCoupons();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  /**加载可领取优惠劵列表 */
  loadOnlineReceviedCoupons(){
    var that=this;
    var param = {
      ProductId: that.data.productId,
      PageIndex: that.data.PageIndex,
      PageSize: that.data.PageSize, 
    };
    coupon.loadOnlineCouponsList(param, function (res) {
      var contentlistTem = that.data.couponList;
      if (that.data.PageIndex == 1) {
        contentlistTem = [];
      }
      if (res.count > 0) {
        var contentlist = res.data;
        console.log(contentlist,'---');
        contentlist.map((item,i)=>{
          coupon.praseCouponStatus(item);
          item.TerminalDesc = coupon.praseTerminalName(item.Terminal);
        });
        if (contentlist.length < that.data.PageSize) {
          that.setData({
            couponList: contentlistTem.concat(contentlist),
            hasMoreData: false
          });
        } else {
          that.setData({
            couponList: contentlistTem.concat(contentlist),
            hasMoreData: true,
            PageIndex: that.data.PageIndex + 1
          });
        }

      } else {
        that.setData({
          couponList: [],
          hasMoreData: true,
          PageIndex: 1
        });
      }
    });
  },
  /**领取优惠劵 */
  receivedCoupon: async function(evt){
    var couponId=evt.currentTarget.dataset.couponid;
    var fkid = evt.currentTarget.dataset.fkid;
    var that=this;
    let type = evt.currentTarget.dataset.type
    let integration = evt.currentTarget.dataset.integration
    if (type == 5) {
      // 积分兑换时，需要用户二次确认是否消耗积分兑换
      let confirmRes = await confirm({
        content: `请确认是否需要使用${integration}积分兑换该优惠券，兑换后积分不会退还，是否兑换？`
      })
      if (!confirmRes.confirm) {
        return
      }
    }
    coupon.onlineReceivedCoupon(couponId, fkid, function (res){

      that.setData({ PageIndex: 1});
      setTimeout(()=>{
        that.loadOnlineReceviedCoupons();
      },1000)
      
    }, type);
  },
  /**
   * 使用优惠券
   */
  onUseEvent: function (e) {
    var index = e.currentTarget.dataset.index;
    var item = this.data.couponList[index];
    app.globalData.product_list.rule_id = item.Id;
    router.navigate({
      path: "productList"
    });
  }
})